﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdaptationProject.Properties;
using AdaptationProject.Utils;
using SharedTypes;

namespace AdaptationProject
{
    public partial class SetupMethodParameters : Form
    {
        private  int FormWidth = 430;
        private const int LeftGroupOffset = 5;
        private const int SatrTopOffset = 10;
        private const int GroupTopOffset = 10;
        private const int LabelInnerHeight = 20;
        private readonly Point TrackBarInnerLocation = new Point(5, 20);
        private readonly Size TrackBarInnerSize = new Size(270, 45);
        private readonly Point LabelInnerLocation = new Point(120, 50);
        private readonly Size GroupSize = new Size(250, 80);
        private Point groupLocation = new Point(LeftGroupOffset, SatrTopOffset);
        private int formHeight = SatrTopOffset;
        private TabPage parent;

        Font fontGB = new Font("Microsoft Sans Serif", 12, FontStyle.Bold, GraphicsUnit.Point);
        Font fontButton = new Font("Georgia", 9);
        private List<ParameterAttribute> currentAlgParameters;
        private object[] customParameters;

        public static int MethodID
        {
            get; set;
        }

        public SetupMethodParameters(TabPage pr)
        {
            parent = pr;
            //FormWidth = parent.Width;
            TrackBarInnerSize = new Size(FormWidth - 30, 45);
            GroupSize = new Size(FormWidth - 15, 90);
            LabelInnerLocation = new Point(FormWidth / 2-10, 65);

            currentAlgParameters = ResContainer.GetDefaultMethodParameters(MethodID);
            if (currentAlgParameters == null)
            {
                this.Hide();
                MessageBox.Show(Resources.SETTINGS_NoParams);
            }
            else
            {
                customParameters = new object[currentAlgParameters.Count];
                foreach (ParameterAttribute currentAlgParameter in currentAlgParameters)
                {
                    AddControlElement(currentAlgParameter);
                }

                Button btn = new Button();
                btn.Text = "Cохранить";
                btn.Width = 82;
                btn.Location = new Point(FormWidth - btn.Width - 10, formHeight + 10);
                btn.Height = 25;
                btn.Font = fontButton;
                btn.Click += (sender, eventArgs) =>
                                 {
                                     ResContainer.SetCustomMethodParameters(MethodID, customParameters.ToList());
                                 };
                parent.Controls.Add(btn);
                formHeight += 20 + btn.Height;
                InitializeComponent();
            }
        }

        private void SetupMethodParameters_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentAlgParameter"></param>
        private void AddControlElement(ParameterAttribute currentAlgParameter)
        {
            GroupBox gb = new GroupBox();
            gb.Font = fontGB;
            gb.Text = currentAlgParameter.Description;
            gb.Location = groupLocation;
            gb.Size = GroupSize;
            groupLocation.Y += (GroupSize.Height + GroupTopOffset);
            formHeight += (GroupSize.Height + GroupTopOffset);

            if (currentAlgParameter.ParamType == typeof(Int32))
            {
                Label lbl = new Label();
                lbl.Location = LabelInnerLocation;
                lbl.Height = LabelInnerHeight;
                lbl.Width = 30;
                TrackBar trackBar = new TrackBar();
                trackBar.Location = TrackBarInnerLocation;
                trackBar.Size = TrackBarInnerSize;
                trackBar.Minimum = (int) currentAlgParameter.MinimumValue;
                trackBar.Maximum = (int) currentAlgParameter.MaximumValue;
                trackBar.Enabled = true;
                trackBar.TickFrequency = 1;
                object currentValue = ResContainer.GetMethodCustomParameterByOrderNum(MethodID,
                                                                                      currentAlgParameter.OrderNum);
                if (currentValue != null)
                {
                    trackBar.Value = (int) currentValue;
                }
                lbl.Text = trackBar.Value.ToString();
                trackBar.ValueChanged += (sender, eventArs) =>
                                             {
                                                 customParameters[currentAlgParameter.OrderNum] =
                                                     (sender as TrackBar).Value;
                                                 lbl.Text = (sender as TrackBar).Value.ToString();
                                             };
                gb.Controls.Add(lbl);
                gb.Controls.Add(trackBar);
                
            }
            else if (currentAlgParameter.ParamType == typeof(Double) || currentAlgParameter.ParamType == typeof(float))
            {
                // TODO: implement another logic, if data type not int
            }
            parent.Controls.Add(gb);
        }
    }
}
